package com.slf.engine.services.helper;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.NameValuePair;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import com.slf.engine.utils.CommonUtil.IntPair;
import com.zhkj.wsaction.type.TPacket;


public class ServiceHelper {

	public static String getHeader(Header[] headers)
	{
		String header = "";
		if(headers != null && headers.length > 0)
		{
			int j = 0;
			for(int i = 0; i < headers.length; i++)
			{
				if(headers[i].getName().equals("Set-Cookie"))
				{
					if(j == 0)
					{
						header += headers[i].getValue().substring(0, headers[i].getValue().indexOf(";"));
					}else{
						header += ";" + headers[i].getValue().substring(0, headers[i].getValue().indexOf(";"));
					}
					j++;
				}
			}
			return header;
		}
		return headers.toString();
	}
	
	public static String cookieStr(Cookie[] cookies)
	{
		String cookie = "";
		if(cookies != null && cookies.length > 0)
		{
			for(int i = 0; i < cookies.length;i++)
			{
				if(i == 0)
				{
					cookie += cookies[i].getName() + "=" + cookies[i].getValue();
				}else{
					cookie += "; " + cookies[i].getName() + "=" + cookies[i].getValue();
				}
				
			}
			return cookie;
		}
		return cookies.toString();
	}
	
	public static void main(String args[])
	{
	}
	
	public static long contentLength(List<NameValuePair> params)
	{
		String s = "";
		for(int i = 0; i < params.size(); i++)
		{
			if(i == 0)
			{
				s += params.get(i).getName() + "=" + params.get(i).getValue();
			}else{
				s += "&" + params.get(i).getName() + "=" + params.get(i).getValue();
			}
		}
		
		return s.length();
	}
	
	public static String acookie_load_id()
	{
		return String.valueOf((long)(Math.floor(Math.random()*9999999999l)+1));
	}
	
	public static String postToJson(List<NameValuePair> list)
	{
		StringBuffer s = new StringBuffer();
		for(int i = 0; i < list.size(); i++)
		{
			NameValuePair valuePair = list.get(i);
			if(i == 0)
			{
				s.append(valuePair.getName() + "=" + valuePair.getValue());
			}else{
				s.append("&" + valuePair.getName() + "=" + valuePair.getValue());
			}
		}
		return s.toString();
	}
	
	public static String postToJson2(List<NameValuePair> list)
	{
		StringBuffer s = new StringBuffer("{");
		try {
			for(int i = 0; i < list.size(); i++)
			{
				NameValuePair valuePair = list.get(i);
				if(i == 0)
				{
					
						s.append("'"+valuePair.getName() + "':'" + URLDecoder.decode(valuePair.getValue(),"utf-8")+"'");
					
				}else{
					s.append(",'" + valuePair.getName() + "':'" + URLDecoder.decode(valuePair.getValue(),"utf-8")+"'");
				}
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		s.append("}");
		return s.toString();
	}
	
	
	
}
